<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>鼠标移动事件</title>
    <style>
      #box1 {
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: red;
      }
    </style>
  </head>
  <body style="height: 1000px">
    <div id="box1"></div>

    <script>
      var box1 = document.getElementById("box1");

      document.onmousemove = function (event) {
        // 解决兼容问题
        event = event || window.event;

        // 获取鼠标的坐标
        /* 
        clientX与clientY代表当前可见窗口的坐标
        pageX与pageY代表当前页面的坐标
         */
        var left = event.pageX;
        var top = event.pageY;
        box1.style.left = left + "px";
        box1.style.top = top + "px";
      };
    </script>
  </body>
</html>
